import {createSlice, configureStore} from '@reduxjs/toolkit';

// 创建切片
const countSlice = createSlice({
    name:'count',
    initialState:{
        num:1
    },
    reducers:{// 定义程序员
        addNum(state, {payload}){
            state.num += payload
        },
        decNum(state, {payload}){
            state.num -= payload
        }
    }
})
const {addNum, decNum} = countSlice.actions;

// 创建数据仓库
const store = configureStore({
    reducer:{
        count:countSlice.reducer
    }
})

// 获取数据仓库中的数据  getState

console.log('store: ', store);
console.log('数据: ', store.getState());
console.log('num: ', store.getState().count.num);

// 修改数据
// 语法： store.dispatch(action对象)
// store.dispatch({type:'count/addNum', payload:3});
// console.log('addNum(3): ', addNum(3));

store.dispatch(addNum(3));
console.log(store.getState());

store.dispatch(decNum(2));
console.log(store.getState());
